package cn.edu.ncu.housinghub.config;

import org.springframework.http.server.ServerHttpRequest;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.server.support.DefaultHandshakeHandler;

import java.security.Principal;
import java.util.Map;

/**
 * WebSocket握手处理器，用于设置用户主体
 */
public class WebSocketUserPrincipalHandshakeHandler extends DefaultHandshakeHandler {

    @Override
    protected Principal determineUser(ServerHttpRequest request, WebSocketHandler wsHandler, Map<String, Object> attributes) {
        // 从握手拦截器设置的属性中获取用户信息
        Object userId = attributes.get("userId");
        Object username = attributes.get("username");
        
        if (userId != null && username != null) {
            return new WebSocketUserPrincipal(userId.toString(), username.toString());
        }
        
        return null;
    }
}
